package com.medical.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.dao.TbAreaMapper;
import com.medical.entity.TbArea;

@Service
@Transactional
public class AreaService {
	@Autowired
	private TbAreaMapper areaMapper;
	
	/**
	 * 添加区域
	 * @param area
	 */
	public void add(TbArea area) {
		area.setCreateTime(new Timestamp(System.currentTimeMillis()));
		area.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		areaMapper.insert(area);
	}
	
	/**
	 * 删除区域
	 * @param id
	 */
	public void delete(Integer id) {
		if (id != null) {
			areaMapper.deleteByPrimaryKey(id);
		}
	}
	
	/**
	 * 修改区域
	 * @param area
	 */
	public void edit(TbArea area) {
		area.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		areaMapper.updateByPrimaryKeySelective(area);
	}
	
	/**
	 * 根据ID主键查找区域信息
	 * @param id
	 * @return
	 */
	public TbArea getAreaById(Integer id) {
		return areaMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 分页查找
	 * @param areaName
	 * @param currentPage
	 * @param limit
	 * @return
	 */
	public PageInfo<TbArea> getAllArea(String areaName, Integer currentPage, Integer limit) {
		//利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<TbArea> list = areaMapper.getAllArea(areaName);
        PageInfo<TbArea> pageInfo = new PageInfo<TbArea>(list);
		return pageInfo;
	}
	
	
	public List<TbArea> getAreaTree(String areaName) {
		return areaMapper.getAllArea(areaName);
	}
	
	

}
